﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.Sql;

namespace ManagementSystem
{
    /// <summary>
    /// Aken uue aine lisamiseks.
    /// </summary>
    public partial class LisaUusAine : Window
    {

        User tempUser = null;
        private ComboBox box;

        /// <summary>
        /// Klassi konstruktor
        /// </summary>
        /// <param name="status">Peaakna statusbar</param>
        /// <param name="user">Kasutaja, kellele tunnid luua</param>
        /// <param name="box">Peaakna combobox</param>
        public LisaUusAine(StatusBar status, User user, ComboBox box)
        {
            InitializeComponent();
            this.status = status;
            tempUser = user;
            this.box = box;
           
        }
        private StatusBar status;

        private bool error;

        
        /// <summary>
        /// Kui vajutatakse nupule "lisa", siis käivitub see meetod.
        /// Lisatakse aine andmebaasi, kui andmed on korrektsed.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            clearLastBorderBrush();

            Aine aine = new Aine();
            if (aineNimiBox.Text.Equals(""))
            {
                aineNimiBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                aine.Name = aineNimiBox.Text;
            }

            error = false;
            if (eapBox.Text.Equals(""))
            {
                eapBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                aine.EAP = Converter(eapBox.Text);
                if (error)
                {
                    eapBox.BorderBrush = new SolidColorBrush(Colors.Red);
                    return;
                }
            }

            if (ainekood.Text.Equals(""))
            {
                ainekood.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                aine.AineKood = ainekood.Text;
            }

            aine.Language = keelecomboBox1.SelectedValue.ToString();

            if (kommentaarBox.Text.Equals(""))
            {
                aine.Comment = "";
            }
            else
            {
                aine.Comment = kommentaarBox.Text;
            }

            aine.User_ID = tempUser.User_ID;
            
            Thread workerThread = new Thread(() => this.saveData(aine));
            workerThread.Start();
            this.Close();
        }

        /// <summary>
        /// Taastab kasutajale tagasiside andmiseks muudetud tekstboxide äärised.
        /// </summary>
        private void clearLastBorderBrush()
        {
            aineNimiBox.BorderBrush = new SolidColorBrush(Colors.Black);
            eapBox.BorderBrush = new SolidColorBrush(Colors.Black);
            ainekood.BorderBrush = new SolidColorBrush(Colors.Black);
            kommentaarBox.BorderBrush = new SolidColorBrush(Colors.Black);
        }

        /// <summary>
        /// Muudab string väärtuse doubeliks.
        /// </summary>
        /// <param name="eap">String väärtus</param>
        /// <returns>Stringu double väärtus</returns>
        private double Converter(string eap)
        {
            try
            {
                double nr;
                error = Double.TryParse(eap,out nr);
                if (nr <= 0 || error == false)
                {
                    throw new Exception("EAP peab olema suurem nullist");
                }
                else
                {
                    error = false;
                    return nr;
                }
            }
            catch (Exception)
            {
                error = true;
                return -1;
            }
        }

        /// <summary>
        /// Salvestatakse uus aine baasi.
        /// </summary>
        /// <param name="aine">Salvestatav aine</param>
        private void saveData(Aine aine)
        {
            try
            {
               
                DataClasses1DataContext kont = new DataClasses1DataContext();
                Subject addSubject = new Subject();
                addSubject.Name = aine.Name;
                addSubject.EAP = (decimal)aine.EAP;
                addSubject.Language = aine.Language;
                addSubject.SubjectCode = aine.AineKood;
                addSubject.Comment = aine.Comment;
                addSubject.User_ID = aine.User_ID;
                kont.Subjects.InsertOnSubmit(addSubject);
                kont.SubmitChanges();
                kont.Dispose();
                

                Action action = new Action(() => { status.Items.Clear(); status.Items.Add("Uus aine lisatud !"); });
                this.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);

                Action action2 = new Action(() => { box.Items.Add(aine.Name); box.SelectedIndex = 0; });
                this.Dispatcher.Invoke(action2, DispatcherPriority.ApplicationIdle);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            
        }
         /// <summary>
         /// Kui vajutatakse nupule välju, käivitub antud meetod.
         /// </summary>
         /// <param name="sender">Sender objekt</param>
         /// <param name="e">Sündmuse parameetrid</param>
        private void v2ljuButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Kui muutub ainenime sees olev tekst.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        private void aineNimiBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            aineNimiBox.BorderBrush = new SolidColorBrush(Colors.Black);
        }

        /// <summary>
        /// Kui muutub eapboxi sisu.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        private void eapBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            eapBox.BorderBrush = new SolidColorBrush(Colors.Black);
        }

        /// <summary>
        /// Kui muutub ainekoodi kasti sisu.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        private void ainekood_TextChanged(object sender, TextChangedEventArgs e)
        {
            ainekood.BorderBrush = new SolidColorBrush(Colors.Black);
        }
    }
}
